#include "sys/socket.h"
#include "netinet/in.h"
#include "PreTestMaic.h"
#include "net/if.h"
#include "sys/types.h"
#include "pthread.h"
#include "stdio.h"
#include "unistd.h"
#include "sys/time.h"
#include "strings.h"
#include "string.h"
#include "sys/ioctl.h"
//#include "linux/in.h"
#include "netdb.h"


struct sockaddr_in host;  

void* cth1(void* p)
{
	sleep(1);
	struct sockaddr_in in4;
	bzero(&in4, sizeof(struct sockaddr_in));
	in4.sin_addr.s_addr = 0;
	in4.sin_port = 13101;
	
	int sock = socket(AF_INET, SOCK_DGRAM, 0);
	if(-1 == sock)
	{
		putToStdout("socket failed\r\n");
		return NULL;
	}
	
	int socksd = socket(AF_INET, SOCK_DGRAM, 0);
	if(-1 == socksd)
	{
		putToStdout("socket failed\r\n");
		return NULL;
	}
	int errCode = 0;
//	errCode = bind(sock, (sockaddr*)&in4, sizeof(sockaddr_in));
//	if(errCode < 0)
//	{
//		putToStdout("bind failed\r\n");
//	}
	in4.sin_port = 13102;
	errCode = bind(socksd, (struct sockaddr*)&in4, sizeof(struct sockaddr_in));
	if(errCode < 0)
	{
		putToStdout("bind failed\r\n");
		return NULL;
	}

	const char* datas = "hello i'm test net thead 1";
	in4.sin_addr = host.sin_addr;
	in4.sin_port = 13101;
	int len = sendto(sock, datas, strlen(datas),0, (struct sockaddr*)&in4, sizeof(struct sockaddr_in));
	if(len < 0)
	{
		 putToStdout("sendto failed\r\n");
	     return NULL;
	}
	char buffer[128] = {0};
	len = recvfrom(socksd, buffer, 128,0, NULL, NULL);
	if(len <= 0)
	{
		 putToStdout("receivefrom  failed\r\n");
	     return NULL;
	}
	putToStdout("received at port:13102=> %s\r\n", buffer);
	sleep(2);
	return NULL;
}

void* cth2(void* p)
{
	sleep(1);
	struct sockaddr_in in4;
	in4.sin_addr.s_addr = 0;
	in4.sin_port = 13102;
	bzero(&in4, sizeof(struct sockaddr_in));


	int sock = socket(AF_INET, SOCK_DGRAM, 0);
	if(-1 == sock)
	{
		putToStdout("socket failed\r\n");
		return NULL;
	}
	
	int socksd = socket(AF_INET, SOCK_DGRAM, 0);
	if(-1 == socksd)
	{
		putToStdout("socket failed\r\n");
		return NULL;
	}
	int errCode = 0;
//	int errCode = bind(sock, (sockaddr*)&in4, sizeof(sockaddr_in));
//	if(errCode < 0)
//	{
//		putToStdout("bind failed\r\n");
//		return NULL;
//	}
	in4.sin_port = 13101;
	errCode = bind(socksd, (struct sockaddr*)&in4, sizeof(struct sockaddr_in));
	if(errCode < 0)
	{
		putToStdout("bind failed\r\n");
		return NULL;
	}

	const char* datas = "hello i'm test net thead 2";
	in4.sin_addr = host.sin_addr;
	in4.sin_port = 13102;
	int len = sendto(sock, datas, strlen(datas), 0, (struct sockaddr*)&in4, sizeof(struct sockaddr_in));
	if(len < 0)
	{
		 putToStdout("sendto failed\r\n");
		return NULL;
	}
	char buffer[128] = {0};
	len = recvfrom(socksd, buffer, 128, 0, NULL, NULL);
	if(len <= 0)
	{
		 putToStdout("receivefrom  failed\r\n");
		return NULL;
	}
	putToStdout("received at port:13101=> %s\r\n", buffer);
	sleep(2);
	return NULL;
}

int netWorkTest()
//int main()
{
	pthread_t pid1,pid2;
	int errCode = 0;
	int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
	struct ifreq req;  
	struct sockaddr_in *phost;  
	bzero(&req, sizeof(struct ifreq));  
	strcpy(req.ifr_name, "eth0");  
	ioctl(sockfd, SIOCGIFADDR, &req);  
	phost = (struct sockaddr_in*)&req.ifr_addr;
	host.sin_addr = phost->sin_addr;

	putToStdout("get eth0 addr is: %x\r\n", host.sin_addr.s_addr);

	errCode = pthread_create(&pid1, NULL, cth1, NULL);
	if(errCode != 0)
	{
		putToStdout("pthread create failed\r\n");
		return -1;
	}
	errCode = pthread_create(&pid2, NULL, cth2, NULL);
	if(errCode != 0)
	{
		putToStdout("pthread create failed\r\n");
		return -1;
	}
	pthread_join(pid1, NULL);
	pthread_join(pid2, NULL);
	return 0;
}

